package com.evaluationsystem.Utils;

import com.evaluationsystem.system.entity.UserInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

/**
 * 获取当前登录用户信息 工具类
 * 2020-3-31
 * gdy
 */
@Component
public class UserContextUtils {

    /*获取当前登录用户信息*/
    public UserInfo getUser(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object principal = authentication.getPrincipal();
        UserInfo user = (UserInfo)principal;
        return user;
    }

    /*获取当前登录用户userid*/
    public String getUserId(){
        String userid = getUser().getUserid();
        return userid;
    }

    // prefix: 前缀，lenght：填充的长度，filler：填充的字符串
    public String getMaxid(String id,String prefix,int lenght,String filler){
        if("1".equals(id)){
            id = prefix + StringUtils.leftPad(id, lenght, filler);
        }else {
            id = prefix + StringUtils.leftPad((Integer.parseInt(id.substring(prefix.length(),id.length())) +1 )+"",lenght,filler);
        }
        return id;
    }


}
